package kliment.display.draw.shape {
	import flash.display.BitmapData;
	import flash.display.CapsStyle;
	import flash.display.GradientType;
	import flash.display.LineScaleMode;
	import flash.display.Shape;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	
	import kliment.display.filters.color.ColorMatrix;
	import kliment.geom.Geom;

	public class ColorWheelShape extends Shape {
		private var _saturation:Number = 0;
		private var _bitmapData:BitmapData;
		
		public function ColorWheelShape(center:Point, radius:Number) {
			super();
			
			var radians:Number;
			var r:Number;
			var g:Number;
			var b:Number;
			var color:Number;
			var matrix:Matrix;
			var x:Number;
			var y:Number;
			var thickness:int;
			
			thickness = 1 + int(radius / 50);
			
			for(var i:int = 0; i < 360; i++) {
				radians = Geom.gradToRad(i);
				
				r = Math.cos(radians) * 127 + 128 << 16;
				g = Math.cos(radians + 2 * Math.PI / 3) * 127 + 128 << 8;
				b = Math.cos(radians + 4 * Math.PI / 3) * 127 + 128;
				
				color = r | g | b;
				
				x = center.x + radius * Math.cos(radians);
				y = center.y + radius * Math.sin(radians);
				
				matrix = new Matrix();
				matrix.createGradientBox(radius * 2, radius * 2, radians, -radius + center.x, -radius + center.y);
				
				graphics.lineStyle(thickness, 0x000000, 1, false, LineScaleMode.NONE, CapsStyle.NONE);
				graphics.lineGradientStyle(GradientType.LINEAR, [0xffffff, color], [100, 100], [127, 255], matrix);
				graphics.moveTo(center.x, center.y);
				graphics.lineTo(x, y);
			}
			graphics.endFill();
		}
		
		private function _drawBirmapData():void {
			if (!_bitmapData){
				_bitmapData = new BitmapData(width, height, true, 0x000000);
				_bitmapData.draw(this);
			}
		}
		
		public function get bitmapData():BitmapData { _drawBirmapData(); return _bitmapData }
		
		public function get saturation():Number { return _saturation }
		public function set saturation(value:Number):void {
			var colorF:ColorMatrix = new ColorMatrix();
			colorF.saturation(value);
			filters = [new ColorMatrixFilter(colorF)];
		}
		
	}
}